import {NativeModules, DeviceEventEmitter} from 'react-native';
const {PdaRFIDModule} = NativeModules;
let isListenerAdded = false;
//let inventoryTagListener =
class RFIDService {
  static async initRFID(callback) {
    try {
      NativeModules.PdaRFIDModule.init(res => callback(res));
    } catch (error) {
      console.error('RFID初始化失败:', error);
      throw error;
    }
  }
  static async isRfidInitialized(callback) {
    PdaRFIDModule.isRfidInitialized()
      .then(isInitialized => {
        callback(isInitialized);
      })
      .catch(error => {
        console.error('Error checking RFID SDK initialization:', error);
      });
  }

  static async checkConnection(callback) {
    PdaRFIDModule.isConnected(isConnected => callback(isConnected));
  }

  static async callbackRFID(callback) {
    // 数据监听的方法

    PdaRFIDModule.registerCallback();
    // 添加监听器并保存返回的订阅对象
    const inventoryTagListener = DeviceEventEmitter.addListener(
      'onInventoryTag',
      function (msg) {
        let epc = msg.epc;
        let data = msg.data;
        let rssi = msg.rssi;
        console.error(
          'onInventoryTag epc:' + epc + '    data:' + data + '    rssi:' + rssi,
        );
        callback(msg);
      },
    );
    DeviceEventEmitter.addListener('onInventoryTagEnd', function (msg) {
      console.error('onInventoryTagEnd ');
    });
    return inventoryTagListener; // 返回监听器对象
  }
  static async unregisterCallbackRFID() {
    // 数据监听的方法
    PdaRFIDModule.unregisterCallback();
    // DeviceEventEmitter.addListener('onInventoryTag', function (msg) {
    //   let epc = msg.epc;
    //   let data = msg.data;
    //   let rssi = msg.rssi;
    //   console.error(
    //     'onInventoryTag epc:' + epc + '    data:' + data + '    rssi:' + rssi,
    //   );
    //   // if(epc){
    //   //   callback(msg);
    //   // }
    //   callback(msg);

    // });
    // DeviceEventEmitter.addListener('onInventoryTagEnd', function (msg) {
    //   console.error('onInventoryTagEnd ');
    // });
  }
  static async startInventory() {
    let session = 0;
    PdaRFIDModule.startInventory(session, res => {
      console.error('startInventory() ' + res);
    });
  }
  static async stopInventory() {
    NativeModules.PdaRFIDModule.stopInventory(res => {
      console.error('stopInventory() ' + res);
    });
  }

  static async inventorySingle(callback) {
    console.log('inventorySingle()');
    PdaRFIDModule.inventorySingle(res => callback(res));
  }
  static async readTagBack() {
    try {
      let epc = '';
      //let epc = '222200A7433530351C5F1C1E'; //Pass in the actual EPC of the label,
      let btMemBank = 1; //0:possword   1: epc  2: tid  3:user
      let btWordAdd = 2; //start address
      let btWordCnt = 3; //Read length
      let btAryPassWord = '00000000'; //Password (default: "00000000")

      NativeModules.PdaRFIDModule.readTag(
        epc,
        btMemBank,
        btWordAdd,
        btWordCnt,
        btAryPassWord,
        res => {
          console.error('readTag() ' + res);
        },
      );
    } catch (error) {
      console.error('RFID读取错误:', error);
      throw error;
    }
  }
  static async readTag(params, callback) {
    try {
      let epc = params.epc || '';
      //let epc = '222200A7433530351C5F1C1E'; //Pass in the actual EPC of the label,
      let btMemBank = params.btMemBank || 1; //0:possword   1: epc  2: tid  3:user
      let btWordAdd = params.btWordAdd; //start address
      let btWordCnt = params.btWordCnt; //Read length
      let btAryPassWord = params.strPassWord; //Password (default: "00000000")

      NativeModules.PdaRFIDModule.readTag(
        epc,
        btMemBank,
        btWordAdd,
        btWordCnt,
        btAryPassWord,
        res => {
          callback(res);
        },
      );
    } catch (error) {
      console.error('RFID读取错误:', error);
      throw error;
    }
  }
  static async writeTag() {
    try {
      let epc = '';
      //let epc  = "222200A7433530351C5F1C1E";//Pass in the actual EPC of the label
      let btMemBank = 1; //0:possword   1: epc  2: tid  3:user
      let btWordAdd = 3; //The start address of the write
      let btAryPassWord = '00000000'; //Password (default: "00000000")
      let btAryData = '1234'; //Written content

      NativeModules.PdaRFIDModule.writeTag(
        epc,
        btAryPassWord,
        btMemBank,
        btWordAdd,
        btAryData,
        res => {
          console.error('writeTag() ' + res);
        },
      );
    } catch (error) {
      console.error('RFID写入错误:', error);
      throw error;
    }
  }
  static async powerON() {
    try {
      NativeModules.PdaRFIDModule.power(true);
      console.log('上电');
    } catch (error) {
      console.error('上电错误:', error);
      throw error;
    }
  }
  static async powerOnAsync() {
    try {
      const res = await NativeModules.PdaRFIDModule.powerAsync(true);
      console.log('上电');
      return res;
    } catch (error) {
      console.error('上电错误:', error);
      throw error;
    }
  }
  static async powerOFF() {
    try {
      NativeModules.PdaRFIDModule.power(false);
    } catch (error) {
      console.error('下电错误:', error);
      //      throw error;
    }
  }
  static disConnect = async () => {
    try {
      const result = await PdaRFIDModule.disConnect();
      console.log(`Disconnect result: ${result}`);
    } catch (error) {
      console.error('Error calling disConnect method:', error); // 错误处理
    }
  };
  static async isConnect() {
    try {
      return NativeModules.PdaRFIDModule.isConnected();
    } catch (error) {
      return false;
      console.error('isConnect错误:', error);
    }
  }
}

export default RFIDService;
